
nginx 配置
本文汇总了 Nginx 的常用配置,包括 gzip 压缩、缓存策略、反向代理、HTTPS SSL 部署以及常见问题处理方案。
nginx
若修改了nginx的启动目录,则需要修改 user属性
1#user nginx;
2user root;
gzip
1# 开启gzip功能
2gzip on;
3# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
4gzip_min_length 1k;
5# 缓存空间大小
6gzip_buffers 4 16k;
7# 设置gzip压缩针对的HTTP协议版本
8gzip_http_version 1.1;
9# 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
10gzip_comp_level 4;
11gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
12# 是否在http header中添加Vary: Accept-Encoding,建议开启
13gzip_vary on;
14# 禁用IE 6 gzip
15gzip_disable "MSIE [1-6]\.";
16
17######cache
18proxy_connect_timeout 6;
19proxy_send_timeout 120;
20proxy_read_timeout 60;
21proxy_buffer_size 16k;
22proxy_buffers 4 64k;
23proxy_busy_buffers_size 128k;
24proxy_temp_file_write_size 128k;
25proxy_temp_path /data/cache/proxy_temp;
26proxy_cache_path /data/cache/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=2g;
27proxy_cookie_path / '/; secure; SameSite=None';
构建纯净的nginx config
1在nginx.conf 最后配置
2include vhost/*.conf;
3之后在当前路径的vhost目录下根据域名新建配置xx.conf即可
4在 vhost下新建cert目录存放证书文件
测试是否通过nginx -t,通过后重启nginx -s reload
1killall nginx
2nginx -c /etc/nginx/nginx.conf
3nginx -s reload
反向代理
1# 使用反向代理 proxy_pass http://host;
2upstream host {
3 server 127.0.0.1:3000;
4}
5server {
6 listen 80;
7 server_name _ host host1; #绑定的域名;_ 直接IP访问
8
9 #access_log /etc/nginx/conf.d/host.log;
10
11 location / {
12 proxy_set_header X-Real-IP $remote_addr;
13 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
14 proxy_set_header Host $http_host;
15 proxy_set_header X-NginX-Proxy true;
16 proxy_set_header Connection "";
17 proxy_http_version 1.1;
18 proxy_pass http://127.0.0.1:3000; #反向代理端口
19
20 ## websocket 不可重定向
21 proxy_redirect off;
22 proxy_http_version 1.1;
23 proxy_set_header Upgrade $http_upgrade;
24 proxy_set_header Connection "upgrade";
25 }
26
27 location ^~ /web {
28 # alias不会使用location后面配置的路径,而且如果alias指定的是目录,后面一定要加上 "/"
29 alias /mnt/projects/smart-farm/web/;
30 index index.html;
31 try_files $uri $uri/ /web/index.html; # history mode
32 }
33
34 location ^~ {
35 return 301 $scheme://$host/web; # 首页重定向到指定页面/web
36 }
37
38}
39
40#保留请求方式转发https
41server {
42 listen 80;
43 server_name host;
44 location / {
45 return 307 https://$host$request_uri;
46 }
47}
48server {
49 listen 443 ssl;
50 server_name host;
51
52 ssl_certificate /etc/nginx/vhost/cert/3247124_host.pem;
53 ssl_certificate_key /etc/nginx/vhost/cert/3247124_host.key;
54
55 ssl_session_timeout 5m;
56 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
57 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
58 ssl_prefer_server_ciphers on;
59 # access_log /var/log/nginx/access_ssl.log main;
60
61 location ^~ /api {
62 proxy_set_header X-Real-IP $remote_addr;
63 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
64 proxy_set_header Host $http_host;
65 proxy_set_header X-NginX-Proxy true;
66 proxy_set_header Connection "";
67 proxy_http_version 1.1;
68 proxy_pass http://127.0.0.1:8778;
69 }
70
71 location ^~ /console {
72 # alias不会使用location后面配置的路径,而且如果alias指定的是目录,后面一定要加上 "/"
73 # alias /root/projects/console/;
74 # projects目录下需要有console目录
75 root /root/projects;
76 index index.html;
77 try_files $uri $uri/ /console/index.html; # history mode
78 }
79
80 location / {
81 root /root/projects/node;
82 index index.html;
83 try_files $uri $uri/ /index.html; # history mode
84 }
85}
https
1server {
2 listen 443 ssl;
3 server_name host;
4 ##ssl on;
5 root /usr/share/nginx/html/host;
6 index index.html;
7 ssl_certificate /usr/local/nginx_ssl/1935715_host.anyrtc.io.pem;
8 ssl_certificate_key /usr/local/nginx_ssl/1935715_host.key;
9 ssl_session_timeout 5m;
10 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
11 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
12 ssl_prefer_server_ciphers on;
13
14 location / {
15 root /usr/share/nginx/html/host;
16 try_files $uri $uri/ /index.html; #history mode
17 proxy_set_header X-Real-IP $remote_addr;
18 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
19 proxy_set_header Host $http_host;
20 proxy_set_header X-NginX-Proxy true;
21 access_log /var/log/nginx/access_ssl.log main;
22 if ($request_uri ~ ^/api/server/userconsole(/)?.*){
23 proxy_pass http://127.0.0.1:7370$request_uri;
24 }
25 if ($request_uri ~ ^/api/servernotify/v1(/)?.*){
26 proxy_pass http://127.0.0.1:7390$request_uri;
27 }
28 }
29}
问题
-
connection to raw.githubusercontent.com:443
在 www.ipaddress.com 查询 raw.githubusercontent.com 的真实为IP 185.199.108.133 (以实际为准) sudo vim /etc/hosts 185.199.108.133 raw.githubusercontent.com :wq
1.
- 标题: nginx 配置
- 作者: Jerome Xiong
- 创建于: 2020-04-01 18:00
- 链接: https://jeromexiong.github.io/2020/04/01/Linux-DevOps/nginx 配置/
- 版权声明:本文章采用 CC BY-NC-SA 4.0 进行许可。
评论